{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组间的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 8])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组间加法和乘法，都遵循广播机制\n",
    "# 数组间的乘法\n",
    "a = np.array([1, 2])\n",
    "b = np.array([3, 4])\n",
    "a * b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  4],\n",
       "        [ 9, 16]],\n",
       "\n",
       "       [[ 5, 12],\n",
       "        [21, 32]]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组间的乘法\n",
    "a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])\n",
    "b = np.array([[1, 2], [3, 4]])\n",
    "# a + b\n",
    "a * b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 2,  3],\n",
       "        [ 5,  6]],\n",
       "\n",
       "       [[ 6,  7],\n",
       "        [ 9, 10]]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([[1], [2]])\n",
    "a + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4],\n",
       "       [6, 8]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Python原生列表的加法和乘法\n",
    "list_a = [1, 2]\n",
    "list_b = [3, 4]\n",
    "# list_a + list_b  # 列表拼接到新列表中\n",
    "# list_a + 1  # 类型不一样，不能相加，报错\n",
    "# list_a * 2  # 复制list_a两次到新列表中\n",
    "list_a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 矩阵一些运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[81.8],\n",
       "       [81.4],\n",
       "       [82.9],\n",
       "       [90. ],\n",
       "       [84.8],\n",
       "       [84.4],\n",
       "       [78.6],\n",
       "       [92.6]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵乘法：pynum.matmul，pynum.dot\n",
    "a = np.array([[80, 86],\n",
    "[82, 80],\n",
    "[85, 78],\n",
    "[90, 90],\n",
    "[86, 82],\n",
    "[82, 90],\n",
    "[78, 80],\n",
    "[92, 94]])\n",
    "\n",
    "b = np.array([[0.7], [0.3]])\n",
    "\n",
    "np.matmul(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[81.8],\n",
       "       [81.4],\n",
       "       [82.9],\n",
       "       [90. ],\n",
       "       [84.8],\n",
       "       [84.4],\n",
       "       [78.6],\n",
       "       [92.6]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.matmul(a, 1)  # 报错，matmul不支持矩阵和数的乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.4],\n",
       "       [0.6]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(b, 2)  # dot支持矩阵和数字的乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
